package cn.sunny360;

import com.microsoft.playwright.*;
import com.microsoft.playwright.options.LoadState;

import java.util.ArrayList;
import java.util.List;

public class Example {
  public static void main(String[] args) {
    try (Playwright playwright = Playwright.create()) {
      Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions()
        .setHeadless(false));
      BrowserContext context = browser.newContext();
      Page page = context.newPage();
      page.navigate("https://kyfw.12306.cn/otn/leftTicket/init?linktypeid=dc&fs=%E5%8C%97%E4%BA%AC,BJP&ts=%E7%9F%B3%E5%AE%B6%E5%BA%84,SJP&date=2024-04-24&flag=N,N,Y#");
//      page.getByRole(AriaRole.TEXTBOX, new Page.GetByRoleOptions().setName("请输入或选择出发地，按键盘上下键进行选择，按回车键选中")).click();
//      page.getByRole(AriaRole.LISTITEM, new Page.GetByRoleOptions().setName("北京")).click();
//      page.getByRole(AriaRole.LISTITEM, new Page.GetByRoleOptions().setName("石家庄")).click();
//      Page page1 = page.waitForPopup(() -> {
//        page.getByRole(AriaRole.LINK, new Page.GetByRoleOptions().setName("查 询")).click();
//      });
//      page1.getByText("04-26").first().click();
//      page1.getByText("04-26 周五").click();
//      page1.pause();
      //page.getByRole(AriaRole.LINK, new Page.GetByRoleOptions().setName("查询").setExact(true)).click();
      page.locator("#query_ticket").click();
      page.waitForLoadState(LoadState.NETWORKIDLE);
      List<Locator> all = page.locator("#queryLeftTable > tr > td div.train a").all();
      List<Locator> disableLocators = new ArrayList<>();
      for (Locator locator : all) {
        locator.scrollIntoViewIfNeeded();
        page.mouse().wheel(0.0, 100.0);

        try {
          locator.click(new Locator.ClickOptions().setTimeout(200));
        } catch (Exception e) {
          e.printStackTrace();
          disableLocators.add(locator);
          continue;
        }
        page.waitForLoadState(LoadState.NETWORKIDLE);
        String trainNum = locator.textContent();
        System.out.print(trainNum + "\t");
        String ftStation = page.locator("#train_span_ > span.item2").textContent();
        System.out.print(ftStation + "\t");
        String trainType = page.locator("#train_span_ > span.item3").textContent();
        System.out.println(trainType);
        List<Locator> trainGrid = page.locator("#train_table_ > tr").all();
        for (Locator tr : trainGrid) {
          List<Locator> tds = tr.locator("td").all();
          for (Locator td : tds) {
            System.out.print(td.textContent() + "\t");
          }
          System.out.println();
        }
      }
      for (Locator locator : disableLocators) {
        locator.scrollIntoViewIfNeeded();
        try {
          locator.click(new Locator.ClickOptions().setClickCount(1));
        } catch (Exception e) {
          continue;
        }
        page.waitForLoadState(LoadState.NETWORKIDLE);
        String trainNum = locator.textContent();
        System.out.print(trainNum + "\t");
        String ftStation = page.locator("#train_span_ > span.item2").textContent();
        System.out.print(ftStation + "\t");
        String trainType = page.locator("#train_span_ > span.item3").textContent();
        System.out.println(trainType);
        List<Locator> trainGrid = page.locator("#train_table_ > tr").all();
        for (Locator tr : trainGrid) {
          List<Locator> tds = tr.locator("td").all();
          for (Locator td : tds) {
            System.out.print(td.textContent() + "\t");
          }
          System.out.println();
        }
      }
//      System.in.read();
      page.pause();
    } catch (Exception e) {
        e.printStackTrace();
    }
  }
}